<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #receiver="{row}">
      <el-tag v-for="v in row.receiver">商户：{{sharing_receiver_map[v.account] || v.account}}分账{{v.amount/100}}元({{v.ratio}}%)</el-tag>
    </template>
    <template #action="{row}">
      <el-button @click="to_edit(row)" size="small">详情</el-button>
    </template>
  </list_url>
</template>
<script>
import {_config} from "@/js/s94_tool.js";
import {in_right_admin} from "@/js/tool.js";
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');

    let status_config = _config('status_alias_map.log_sharing.status');
    let pay_type_map = {
      wechat: '微信支付',
      wechat_partner: '微信服务商',
    }
    let forms_config = {
       account: {label:'接收方',name:'where[receiver_json][like]',type:'select_one',config:[],value:''},
    };
    let list_cols = {
      id:{name:'id', label:'ID', width:'65px', fixed:'left', type: 'hidden', order:true},
      sharing_no: {name:'sharing_no', label:'分账单号', type:'text_one', width:'180px'},
      pay_money: {name:'pay_money', label:'支付金额', type:'text_one', width:'80px'},
      pay_type:{name:'pay_type', label:'支付方式', width:'100px', type:'select_one', config: pay_type_map},
      sharing_money: {name:'sharing_money', label:'分账金额', type:'text_one', width:'80px'},
      status:{name:'status', label:'状态', width: '60px', type:'select_one',config:status_config},
      receiver: {name:'receiver', label:'分账情况', type:'receiver'},
      pay_time: {name:'pay_time', label:'支付时间', type:'text_one', width:'145px'},
      sharing_time: {name:'sharing_time', label:'分账时间', type:'text_one', width:'145px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    if (!_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
    // list_cols['action'] = {name:'action', label:'操作', width:'80px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/log_sharing/'+(is_del?'recycle':'list'),
        title: "分账记录列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          Object.values(list).forEach(row=>{
            row.receiver = JSON.parse(row.receiver_json);
            if (!row.receiver) row.receiver = [];
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
      sharing_receiver_map: {},
    }
  },
  methods: {
    to_edit(row){
      let can_edit = in_right_admin('OrderSharing/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      let param = {
        id: row.id,
      }
      if (!can_edit) param['is_info'] = 1;
      this.iframe('edit?'+s94Mjs.param_encode(param))
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/sharing_receiver/list',cache:true},(res)=>{
      let config = {};
      res.data.list.forEach(function (v){
        config[v.account] = v.name;
      })
      this.sharing_receiver_map = config;
      this.config.forms_config.account.config = config;
      return false;
    })
  }
}
</script>
